package cn.ultrarisk.scadmin.manager.biz.solr.service;

import cn.ultrarisk.scadmin.manager.biz.common.context.OperationType;
import cn.ultrarisk.scadmin.manager.biz.common.service.OperationLogService;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrReplicaDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dto.CollectionAnalyzer;
import cn.ultrarisk.scadmin.manager.biz.solr.dto.CollectionDeleter;
import cn.ultrarisk.scadmin.manager.biz.solr.dto.CollectionQuery;
import cn.ultrarisk.scadmin.manager.biz.solr.dto.CollectionUpdater;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrReplica;
import cn.ultrarisk.scadmin.manager.util.RandomUtil;
import cn.ultrarisk.scadmin.manager.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * SolrDataService
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/6/13
 * @since V1.0
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class SolrDataService {
    @Autowired
    private SolrReplicaDAO solrReplicaDAO;
    @Autowired
    private OperationLogService operationLogService;

    public String update(Integer collectionId, CollectionUpdater updater) throws IOException {
        List<SolrReplica> replicas = solrReplicaDAO.findAvailable(null, collectionId, false);
        SolrReplica replica = replicas.get(RandomUtil.randomInt(0, replicas.size() - 1));
        String result = WebUtil.post(solrReplicaDAO.getUpdateUrl(replica.getId()), updater.getParams(), updater.getPostData(), updater.getContentType());
        operationLogService.createSolrDataLog(OperationType.SOLR_DATA_UPDATE, collectionId);
        return result;
    }

    public String delete(Integer collectionId, CollectionDeleter deleter) throws IOException {
        List<SolrReplica> replicas = solrReplicaDAO.findAvailable(null, collectionId, false);
        SolrReplica replica = replicas.get(RandomUtil.randomInt(0, replicas.size() - 1));
        String result = WebUtil.post(solrReplicaDAO.getUpdateUrl(replica.getId()), deleter.getParams(), deleter.getPostData(), deleter.getContentType());
        operationLogService.createSolrDataLog(OperationType.SOLR_DATA_DELETE, collectionId);
        return result;
    }

    public String analysis(Integer collectionId, CollectionAnalyzer analyzer) throws IOException {
        List<SolrReplica> replicas = solrReplicaDAO.findAvailable(null, collectionId, false);
        SolrReplica replica = replicas.get(RandomUtil.randomInt(0, replicas.size() - 1));
        return WebUtil.get(solrReplicaDAO.getAnalysisUrl(replica.getId()), analyzer.getParams());
    }

    public String query(Integer collectionId, CollectionQuery query) throws IOException {
        List<SolrReplica> replicas = solrReplicaDAO.findAvailable(null, collectionId, false);
        SolrReplica replica = replicas.get(RandomUtil.randomInt(0, replicas.size() - 1));
        return WebUtil.get(solrReplicaDAO.getSelectUrl(replica.getId()), query.getParams());
    }
}
